﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataContexts.Supplier;
using System.IO;

public partial class Supplier_Vehicle_VehicleImage : System.Web.UI.Page
{
    SupplierDataContext context = new SupplierDataContext();

    public int VehicleId
    {
        get
        {
            return Helper.QSIntValue("id");
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        ((BaseMasterPage)this.Master).CurrentPage = PageConfig.Pages.SupplierVehicle;
        if (!((BaseMasterPage)this.Master).AuthorizeCurrentUser())
            return;

        divMessage.InnerHtml = "";
        imgVehicleImage.ImageUrl = "/VehicleImage.ashx?vid=" + VehicleId.ToString();
        SupplierVehicle vehicle = context.SupplierVehicles.FirstOrDefault(p => p.SupplierVehicleId == VehicleId);
        lnkRemove.Visible = (vehicle.VehicleImage != null);
        imgVehicleImage.Visible = (vehicle.VehicleImage != null);
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (IsValidImage())
        {

            SupplierVehicle vehicle = context.SupplierVehicles.FirstOrDefault(p => p.SupplierVehicleId == VehicleId);
            if (vehicle != null)
            {
                vehicle.ImageName = Path.GetFileName(fluVehicleImage.PostedFile.FileName);
                BinaryReader reader = new BinaryReader(fluVehicleImage.PostedFile.InputStream);
                vehicle.VehicleImage = reader.ReadBytes(fluVehicleImage.PostedFile.ContentLength);
                vehicle.ImageType = fluVehicleImage.PostedFile.ContentType;
                context.SubmitChanges();
            }

            Response.Redirect("VehicleImage.aspx?id=" + VehicleId.ToString());
        }
    }
    private bool IsValidImage()
    {
        string ext = Path.GetExtension(fluVehicleImage.PostedFile.FileName).ToLower();
        string exts = "|.jpg|.gif|.png|";
        if (!exts.Contains(ext) || fluVehicleImage.PostedFile.ContentLength == 0)
        {
            divMessage.InnerHtml = "Invalid vehicle image <br/><br/>";
            return false;
        }
        return true;
    }
    protected void lnkRemove_Click(object sender, EventArgs e)
    {
        SupplierVehicle vehicle = context.SupplierVehicles.FirstOrDefault(p => p.SupplierVehicleId == VehicleId);
        if (vehicle != null)
        {
            vehicle.ImageName = string.Empty;
            vehicle.VehicleImage = null;
            vehicle.ImageType = string.Empty;
            context.SubmitChanges();
        }

        Response.Redirect("VehicleImage.aspx?id=" + VehicleId.ToString());
    }
    protected void lnkBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx");
    }
}
